<template>
  <!-- 导航部分 -->
  <el-row>
    <el-col :span="24">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>首页</el-breadcrumb-item>
        <el-breadcrumb-item>质量管理</el-breadcrumb-item>
        <el-breadcrumb-item>检测模板</el-breadcrumb-item>
      </el-breadcrumb>
    </el-col>
  </el-row>

  <!-- 搜索栏 -->
  <el-row style="margin-top: 10px">
    <el-col :span="24">
      <el-form :model="testTemplateQuery" inline>
        <el-form-item label="检测模板编号">
          <el-input v-model="testTemplateQuery.templateCode"/>
        </el-form-item>
        <el-form-item label="检测模板名称">
          <el-input v-model="testTemplateQuery.templateName"/>
        </el-form-item>
        <el-form-item label="产品编码">
          <el-input />
        </el-form-item>
        <el-form-item label="产品名称">
          <el-input />
        </el-form-item>

        <el-form-item label="检测种类">
          <el-checkbox-group v-model="testTemplateQuery.testTypes">
            <el-checkbox label="[来料检验]" value="[来料检验]" />
            <el-checkbox label="[过程检验]" value="[过程检验]" />
            <el-checkbox label="[首检]" value="[首检]" />
            <el-checkbox label="[末检]" value="[末检]" />
            <el-checkbox label="[巡检]" value="[巡检]" />
            <el-checkbox label="[自检]" value="[自检]" />
            <el-checkbox label="[成品检验]" value="[成品检验]" />
            <el-checkbox label="[发货检验]" value="[发货检验]" />
          </el-checkbox-group>
        </el-form-item>
        <el-form-item>
          <el-button @click="loadTestTemplate">搜索</el-button>
        </el-form-item>
        <el-form-item>
          <el-button @click="resetTestTemplate">重置</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>


  <!-- 工具栏 -->
  <el-row style="margin-top: 10px">
    <el-col :span="24">
      <el-button type="primary" @click="toAdd">新增</el-button>
      <el-button type="primary" @click="toRelease">修改</el-button>
      <el-button type="primary" @click="toDeletes">删除</el-button>
    </el-col>
  </el-row>


  <!-- 数据表 -->
  <el-row style="margin-top: 10px">
    <el-col :span="24">
      <el-table
          :data="testTemplates"
          border
          stripe ref="testTestTemplate"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column  label="检测模板编号">
            <template #default="scope">
              <el-link @click="showDetail(scope.row)" type="success">
                {{ scope.row.templateCode}}
              </el-link>
            </template>
        </el-table-column>
        <el-table-column prop="templateName" label="检测模板名称"/>
        <el-table-column prop="testTypes" label="检测种类"/>
        <el-table-column prop="enableFlag" label="是否启用">
          <template #default="scope">
            <el-tag type="success" v-if="scope.row.enableFlag == 'Y'">是</el-tag>
            <el-tag type="success" v-else-if="scope.row.enableFlag == 'N'">否</el-tag>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="330">
          <template #default="scope">
            <el-button-group>
              <el-button size="small" type="primary" @click="toRelease(scope.row.templateId)">修改</el-button>
              <el-button size="small" type="primary" @click="toDelete(scope.row.templateId)">删除</el-button>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>
    </el-col>
  </el-row>


  <!-- 新增的弹窗 -->
  <el-dialog v-model="testTemplateAddWinDialogVisible"
             title="新增检测模板"
             width="50%"
  >

    <el-form :model="newTestTemplate">


      <el-form-item label="模板编码">
        <el-input v-model="newTestTemplate.templateCode"/>
      </el-form-item>

      <el-form-item label="模板名称">
        <el-input v-model="newTestTemplate.templateName"/>
      </el-form-item>

      <el-form-item label="检测种类">
        <el-checkbox-group v-model="testTypesArr">
          <el-checkbox label="[来料检验]" value="[来料检验]" />
          <el-checkbox label="[过程检验]" value="[过程检验]" />
          <el-checkbox label="[首检]" value="[首检]" />
          <el-checkbox label="[末检]" value="[末检]" />
          <el-checkbox label="[巡检]" value="[巡检]" />
          <el-checkbox label="[自检]" value="[自检]" />
          <el-checkbox label="[成品检验]" value="[成品检验]" />
          <el-checkbox label="[发货检验]" value="[发货检验]" />
        </el-checkbox-group>
      </el-form-item>



      <el-form-item label="是否启用">
        <el-radio-group v-model="newTestTemplate.enableFlag">
          <el-radio :value="'Y'" label="是"/>
          <el-radio :value="'N'" label="否"/>
        </el-radio-group>
      </el-form-item>


      <el-form-item label="备注">
        <el-input v-model="newTestTemplate.remark"/>
      </el-form-item>




    </el-form>

    <el-button @click="dialogVisible">取消</el-button>
    <el-button type="primary" @click="addTestTemplateSubmit">
      提交
    </el-button>


  </el-dialog>



  <!-- 编辑的弹窗 -->
  <el-dialog v-model="testTemplateUpdateWinDialogVisible"
             title="修改检测模板"
             width="65%"
  >

    <el-form :model="updateTestTemplate">


      <el-form-item label="模板编码">
        <el-input v-model="updateTestTemplate.templateCode"/>
      </el-form-item>

      <el-form-item label="模板名称">
        <el-input v-model="updateTestTemplate.templateName"/>
      </el-form-item>

      <el-form-item label="检测种类">
        <el-checkbox-group v-model="updateTestTypesArr">
          <el-checkbox label="[来料检验]" value="[来料检验]" />
          <el-checkbox label="[过程检验]" value="[过程检验]" />
          <el-checkbox label="[首检]" value="[首检]" />
          <el-checkbox label="[末检]" value="[末检]" />
          <el-checkbox label="[巡检]" value="[巡检]" />
          <el-checkbox label="[自检]" value="[自检]" />
          <el-checkbox label="[成品检验]" value="[成品检验]" />
          <el-checkbox label="[发货检验]" value="[发货检验]" />
        </el-checkbox-group>
      </el-form-item>



      <el-form-item label="是否启用">
        <el-radio-group v-model="updateTestTemplate.enableFlag">
          <el-radio :value="'Y'" label="是"/>
          <el-radio :value="'N'" label="否"/>
        </el-radio-group>
      </el-form-item>


      <el-form-item label="备注">
        <el-input v-model="updateTestTemplate.remark"/>
      </el-form-item>

    </el-form>




    <el-tabs type="border-card">
      <el-tab-pane label="检测项">

        <!-- 工具栏 -->
        <el-row style="margin-top: 10px">
          <el-col :span="24">
            <el-button type="primary" @click="toAddTemplateIndex">新增</el-button>
            <el-button type="primary" @click="toRelease">修改</el-button>
            <el-button type="primary" @click="toDeletes">删除</el-button>
          </el-col>
        </el-row>


        <!-- 检测项数据表 -->
        <el-row style="margin-top: 10px">
          <el-col :span="24">
            <el-table
                :data="templateIndexs"
                border
                stripe ref="templateIndex"
            >
              <el-table-column type="selection" width="55" />
              <el-table-column prop="indexName" label="检测项名称"/>
              <el-table-column prop="indexType" label="检测项类型">
                <template #default="scope">
                  <el-tag type="success" v-if="scope.row.indexType == 1">尺寸</el-tag>
                  <el-tag type="success" v-else-if="scope.row.indexType == 2">外观</el-tag>
                  <el-tag type="success" v-else-if="scope.row.indexType == 3">重量</el-tag>
                  <el-tag type="success" v-else-if="scope.row.indexType == 4">性能</el-tag>
                  <el-tag type="success" v-else-if="scope.row.indexType == 5">成分</el-tag>

                </template>
              </el-table-column>
              <el-table-column prop="qcTool" label="检测工具"/>
              <el-table-column prop="checkMethod" label="检测方法"/>
              <el-table-column prop="standerVal" label="标准值"/>
              <el-table-column prop="unitOfMeasure" label="单位"/>
              <el-table-column prop="thresholdMax" label="误差上限"/>
              <el-table-column prop="thresholdMin" label="误差下限"/>

              <el-table-column label="操作" width="330">
                <template #default="scope">
                  <el-button-group>
                    <el-button size="small" type="primary" @click="toReleaseTemplateIndex(scope.row.recordId)">修改</el-button>
                    <el-button size="small" type="primary" @click="toDelete(scope.row.templateId)">删除</el-button>
                  </el-button-group>
                </template>
              </el-table-column>
            </el-table>
          </el-col>
        </el-row>

      </el-tab-pane>
      <el-tab-pane label="物料产品">
        <!-- 工具栏 -->
        <el-row style="margin-top: 10px">
          <el-col :span="24">
            <el-button type="primary" @click="toAddTemplateProduct">新增</el-button>
            <el-button type="primary" @click="toRelease">修改</el-button>
            <el-button type="primary" @click="toDeletes">删除</el-button>
          </el-col>
        </el-row>


        <!-- 物料产品数据表 -->
        <el-row style="margin-top: 10px">
          <el-col :span="24">
            <el-table
                :data="templateProducts"
                border
                stripe ref="templateProduct"
            >
              <el-table-column type="selection" width="55" />
              <el-table-column prop="itemCode" label="产品物料编码"/>
              <el-table-column prop="itemName" label="产品物料名称"/>
              <el-table-column prop="specification" label="产品型号"/>
              <el-table-column prop="quantityCheck" label="最低检测数"/>
              <el-table-column prop="unitOfMeasure" label="单位"/>
              <el-table-column prop="quantityUnqualified" label="最大不合格数"/>
              <el-table-column prop="crRate" label="最大致命缺陷率"/>
              <el-table-column prop="majRate" label="最大严重缺陷率"/>
              <el-table-column prop="minRate" label="最大轻微缺陷率"/>


              <el-table-column label="操作" width="200">
                <template #default="scope">
                  <el-button-group>
                    <el-button size="small" type="primary" @click="toReleaseTemplateProduct(scope.row.recordId)">修改</el-button>
                    <el-button size="small" type="primary" @click="toDelete(scope.row.templateId)">删除</el-button>
                  </el-button-group>
                </template>
              </el-table-column>
            </el-table>
          </el-col>
        </el-row>
      </el-tab-pane>
    </el-tabs>





    <el-button @click="updateDialogVisible">取消</el-button>
    <el-button type="primary" @click="updateTestTemplateSubmit">
      提交
    </el-button>


  </el-dialog>



  <!-- 检测项新增的弹窗 -->
  <el-dialog v-model="templateIndexAddWinDialogVisible"
             title="新增检测项"
             width="50%"
  >

    <el-form :model="newTemplateIndex">


      <el-form-item label="检测项">
        <el-input v-model="newTemplateIndex.indexName"/>
      </el-form-item>

      <el-form-item label="检测工具">
        <el-input v-model="newTemplateIndex.qcTool"/>
      </el-form-item>

      <el-form-item label="检测要求">
        <el-input v-model="newTemplateIndex.checkMethod"/>
      </el-form-item>

      <el-form-item label="标准值">
        <el-input v-model="newTemplateIndex.standerVal"/>
      </el-form-item>

      <el-form-item label="单位">
        <el-input v-model="newTemplateIndex.unitOfMeasure"/>
      </el-form-item>

      <el-form-item label="误差上限">
          <el-input-number v-model="newTemplateIndex.thresholdMax"/>
      </el-form-item>

      <el-form-item label="误差下限">
          <el-input-number v-model="newTemplateIndex.thresholdMin" />
      </el-form-item>

      <el-form-item label="说明图">
        <el-input v-model="newTemplateIndex.docUrl"/>
      </el-form-item>

      <el-form-item label="备注">
        <el-input v-model="newTemplateIndex.remark"/>
      </el-form-item>


    </el-form>

    <el-button @click="templateIndexDialogVisible">取消</el-button>
    <el-button type="primary" @click="addTemplateIndexSubmit">
      提交
    </el-button>


  </el-dialog>



  <!-- 物料生产新增的弹窗 -->
  <el-dialog v-model="templateProductAddWinDialogVisible"
             title="新增物料生产"
             width="50%"
  >

    <el-form :model="newTemplateProduct">


      <el-form-item label="产品物料编码">
        <el-input v-model="newTemplateProduct.itemCode"/>
      </el-form-item>

      <el-form-item label="产品物料名称">
        <el-input v-model="newTemplateProduct.itemName"/>
      </el-form-item>

      <el-form-item label="规格名称">
        <el-input v-model="newTemplateProduct.specification"/>
      </el-form-item>

      <el-form-item label="最低检测数">
        <el-input-number v-model="newTemplateProduct.quantityCheck" min="1"/>
      </el-form-item>

      <el-form-item label="最大不合格数">
        <el-input-number v-model="newTemplateProduct.quantityUnqualified" min="1"/>
      </el-form-item>

      <el-form-item label="单位">
        <el-input v-model="newTemplateProduct.unitOfMeasure"/>
      </el-form-item>

      <el-form-item label="最大致命缺陷率">
        <el-input-number v-model="newTemplateProduct.crRate" min="1"/>
      </el-form-item>

      <el-form-item label="最大严重缺陷率">
        <el-input-number v-model="newTemplateProduct.majRate" min="1"/>
      </el-form-item>

      <el-form-item label="最大轻微缺陷率">
        <el-input-number v-model="newTemplateProduct.minRate" min="1"/>
      </el-form-item>

      <el-form-item label="备注">
        <el-input v-model="newTemplateProduct.remark"/>
      </el-form-item>


    </el-form>

    <el-button @click="templateProductDialogVisible">取消</el-button>
    <el-button type="primary" @click="addTemplateProductSubmit">
      提交
    </el-button>


  </el-dialog>



  <!-- 检测项编辑的弹窗 -->
  <el-dialog v-model="templateIndexUpdateWinDialogVisible"
             title="编辑检测项"
             width="50%"
  >

    <el-form :model="updateTemplateIndex">


      <el-form-item label="检测项">
        <el-input v-model="updateTemplateIndex.indexName"/>
      </el-form-item>

      <el-form-item label="检测工具">
        <el-input v-model="updateTemplateIndex.qcTool"/>
      </el-form-item>

      <el-form-item label="检测要求">
        <el-input v-model="updateTemplateIndex.checkMethod"/>
      </el-form-item>

      <el-form-item label="标准值">
        <el-input v-model="updateTemplateIndex.standerVal"/>
      </el-form-item>

      <el-form-item label="单位">
        <el-input v-model="updateTemplateIndex.unitOfMeasure"/>
      </el-form-item>

      <el-form-item label="误差上限">
        <el-input-number v-model="updateTemplateIndex.thresholdMax"/>
      </el-form-item>

      <el-form-item label="误差下限">
        <el-input-number v-model="updateTemplateIndex.thresholdMin" />
      </el-form-item>

      <el-form-item label="说明图">
        <el-input v-model="updateTemplateIndex.docUrl"/>
      </el-form-item>

      <el-form-item label="备注">
        <el-input v-model="updateTemplateIndex.remark"/>
      </el-form-item>


    </el-form>

    <el-button @click="updateTemplateIndexDialogVisible">取消</el-button>
    <el-button type="primary" @click="updateTemplateIndexSubmit">
      提交
    </el-button>


  </el-dialog>





  <!-- 物料产品编辑的弹窗 -->
  <el-dialog v-model="templateProductUpdateWinDialogVisible"
             title="编辑产品物料"
             width="50%"
  >

    <el-form :model="updateTemplateProduct">


      <el-form-item label="产品物料编码">
        <el-input v-model="updateTemplateProduct.itemCode"/>
      </el-form-item>

      <el-form-item label="产品物料名称">
        <el-input v-model="updateTemplateProduct.itemName"/>
      </el-form-item>

      <el-form-item label="规格名称">
        <el-input v-model="updateTemplateProduct.specification"/>
      </el-form-item>

      <el-form-item label="最低检测数">
        <el-input-number v-model="updateTemplateProduct.quantityCheck" min="1"/>
      </el-form-item>

      <el-form-item label="最大不合格数">
        <el-input-number v-model="updateTemplateProduct.quantityUnqualified" min="1"/>
      </el-form-item>

      <el-form-item label="单位">
        <el-input v-model="updateTemplateProduct.unitOfMeasure"/>
      </el-form-item>

      <el-form-item label="最大致命缺陷率">
        <el-input-number v-model="updateTemplateProduct.crRate" min="1"/>
      </el-form-item>

      <el-form-item label="最大严重缺陷率">
        <el-input-number v-model="updateTemplateProduct.majRate" min="1"/>
      </el-form-item>

      <el-form-item label="最大轻微缺陷率">
        <el-input-number v-model="updateTemplateProduct.minRate" min="1"/>
      </el-form-item>

      <el-form-item label="备注">
        <el-input v-model="updateTemplateProduct.remark"/>
      </el-form-item>


    </el-form>

    <el-button @click="updateTemplateProductDialogVisible">取消</el-button>
    <el-button type="primary" @click="updateTemplateProductSubmit">
      提交
    </el-button>


  </el-dialog>







  <!-- 详情的弹窗 -->
  <el-dialog v-model="testTemplateListWinDialogVisible"
             title="任务详情"
             width="65%"
  >

    <el-form >


      <el-form-item label="模板编码">
        <el-input v-model="rowTestTemplate.templateCode" readonly="true"/>
      </el-form-item>

      <el-form-item label="模板名称">
        <el-input v-model="rowTestTemplate.templateName" readonly="true"/>
      </el-form-item>

      <el-form-item label="检测种类">
        <el-checkbox-group v-model="rowTestTypeArr" readonly="true">
          <el-checkbox label="[来料检验]" value="[来料检验]" />
          <el-checkbox label="[过程检验]" value="[过程检验]" />
          <el-checkbox label="[首检]" value="[首检]" />
          <el-checkbox label="[末检]" value="[末检]" />
          <el-checkbox label="[巡检]" value="[巡检]" />
          <el-checkbox label="[自检]" value="[自检]" />
          <el-checkbox label="[成品检验]" value="[成品检验]" />
          <el-checkbox label="[发货检验]" value="[发货检验]" />
        </el-checkbox-group>
      </el-form-item>



      <el-form-item label="是否启用">
        <el-radio-group v-model="rowTestTemplate.enableFlag" disabled value="disabled">
          <el-radio :value="'Y'" label="是"/>
          <el-radio :value="'N'" label="否"/>
        </el-radio-group>
      </el-form-item>


      <el-form-item label="备注">
        <el-input v-model="rowTestTemplate.remark" readonly="true"/>
      </el-form-item>


      <el-tabs type="border-card">
        <el-tab-pane label="检测项">
          <!-- 数据表 -->
          <el-row style="margin-top: 10px">
            <el-col :span="24">
              <el-table
                  :data="templateIndexs"
                  border
                  stripe ref="templateIndex"
              >
                <el-table-column type="selection" width="55" />
                <el-table-column prop="indexName" label="检测项名称"/>
                <el-table-column prop="indexType" label="检测项类型">
                  <template #default="scope">
                    <el-tag type="success" v-if="scope.row.indexType == 1">尺寸</el-tag>
                    <el-tag type="success" v-else-if="scope.row.indexType == 2">外观</el-tag>
                    <el-tag type="success" v-else-if="scope.row.indexType == 3">重量</el-tag>
                    <el-tag type="success" v-else-if="scope.row.indexType == 4">性能</el-tag>
                    <el-tag type="success" v-else-if="scope.row.indexType == 5">成分</el-tag>

                  </template>
                </el-table-column>
                <el-table-column prop="qcTool" label="检测工具"/>
                <el-table-column prop="checkMethod" label="检测方法"/>
                <el-table-column prop="standerVal" label="标准值"/>
                <el-table-column prop="unitOfMeasure" label="单位"/>
                <el-table-column prop="thresholdMax" label="误差上限"/>
                <el-table-column prop="thresholdMin" label="误差下限"/>
              </el-table>
            </el-col>
          </el-row>

        </el-tab-pane>
        <el-tab-pane label="物料产品">111111111111111</el-tab-pane>
      </el-tabs>


    </el-form>

  </el-dialog>











  <!-- 分页栏 -->
  <el-row style="margin-top: 10px">
    <!-- 监听页码的变化事件 -->
    <el-pagination @change="changePage" :total="total" background layout="prev, pager, next"/>
  </el-row>



</template>

<script setup>
import {onBeforeUnmount, onMounted, ref, shallowRef} from "vue";
import testTemplateRequest from "@/api/testTemplate.js";
import templateIndexRequest from "@/api/templateIndex.js";
import templateProductRequest from "@/api/templateProduct.js";
import defectRequest from "@/api/defect.js"
import {ElMessageBox} from "element-plus";
import {ElMessage} from "element-plus";
// 封装查询条件
let testTemplateQuery = ref({});
// 封装查询的结果集合
let testTemplates = ref([])
//保存前端多选的数据
let defectTable = ref(null);
// 封装当前的页面
let curPage = ref(1);
// 封装查询的数据总条数
let total = ref(0);

//数据表
function loadTestTemplate(){
  testTemplateRequest.loadTestTemplates(testTemplateQuery.value,curPage.value,10,res =>{
    total.value = res.data.total;
    testTemplates.value = res.data.list;
  })
}

//封装检测项搜索条件
let templateIndexQuery = ref({})
//封装检测项查询数据表
let templateIndexs = ref([])
//检测项数据表
function loadTemplateIndex(templateId){
    templateIndexRequest.loadTemplateIndexs(templateId,curPage.value,10,res =>{
    total.value = res.data.total;
    templateIndexs.value = res.data.list;
  })
}


//封装生产物料查询数据表
let templateProducts = ref([])
//生产物料数据表
function loadTemplateProduct(templateId){
  templateProductRequest.loadTemplateProducts(templateId,curPage.value,10,res =>{
    total.value = res.data.total;
    templateProducts.value = res.data.list;
  })
}



//重置按钮
function resetTestTemplate(){
  testTemplates.value = null;
  loadTestTemplate();
}

function changePage(value){
  curPage.value = value;
  loadTestIndex();
}


//新增弹窗标记
let testTemplateAddWinDialogVisible = ref(false);
//保存新增数据
let newTestTemplate = ref({testTypes: null});
//新增类型数据保存数组
let testTypesArr = ref([]);

//新增
function toAdd(){

  testTemplateAddWinDialogVisible.value = true;
}

//取消
function dialogVisible(){
  testTemplateAddWinDialogVisible.value = false;
}

//提交
function addTestTemplateSubmit(){
  newTestTemplate.value.testTypes = testTypesArr.value.join(',');
  testTemplateRequest.addTestTemplate(newTestTemplate.value,res => {

    if (res.code == 200) {
      ElMessage({
        type: "success",
        message: "新增成功"
      });
      newTestTemplate.value = {testTypes: null};
      testTypesArr.value = [];
      // 关闭窗体
      dialogVisible();
      // 刷新表格
      loadTestTemplate();
    } else {
      ElMessage({
        type: "error",
        message: "新增失败"
      });
    }
  });
}



//检测项新增弹窗标记
let templateIndexAddWinDialogVisible = ref(false);
//检测项保存新增数据
let newTemplateIndex = ref({});
//检测项新增类型数据保存数组
// let tempArr = ref([]);

//检测项新增
function toAddTemplateIndex(){
  newTemplateIndex.value = ref({});
  templateIndexAddWinDialogVisible.value = true;
}

//检测项取消
function templateIndexDialogVisible(){
  templateIndexAddWinDialogVisible.value = false;
}

//检测项提交
function addTemplateIndexSubmit(){
  newTemplateIndex.value.templateId = updateTestTemplate.value.templateId;
  templateIndexRequest.addTemplateIndex(newTemplateIndex.value,res => {
    if (res.code == 200) {
      ElMessage({
        type: "success",
        message: "新增成功"
      });

      // 关闭窗体
      templateIndexDialogVisible();
      // 刷新表格
      loadTemplateIndex(updateTestTemplate.value.templateId);
    } else {
      ElMessage({
        type: "error",
        message: "新增失败"
      });
    }
  });
}





//物料产品新增弹窗标记
let templateProductAddWinDialogVisible = ref(false);
//物料产品保存新增数据
let newTemplateProduct = ref({});
//物料产品新增类型数据保存数组
// let tempArr = ref([]);

//物料产品新增
function toAddTemplateProduct(){
  newTemplateProduct.value = ref({});
  templateProductAddWinDialogVisible.value = true;
}

//物料产品取消
function templateProductDialogVisible(){
  templateProductAddWinDialogVisible.value = false;
}

//物料产品提交
function addTemplateProductSubmit(){
  newTemplateProduct.value.templateId = updateTestTemplate.value.templateId;
  console.log(updateTestTemplate.value.templateId)
  templateProductRequest.addTemplateProduct(newTemplateProduct.value,res => {
    if (res.code == 200) {
      ElMessage({
        type: "success",
        message: "新增成功"
      });
      // 关闭窗体
      templateProductDialogVisible();
      // 刷新表格
      loadTemplateProduct(updateTestTemplate.value.templateId);
    } else {
      ElMessage({
        type: "error",
        message: "新增失败"
      });
    }
  });
}










//行编辑弹窗标记
let testTemplateUpdateWinDialogVisible = ref(false);
//保存编辑内容
let updateTestTemplate = ref([]);
//编辑回显类型数据保存数组
let updateTestTypesArr = ref({});

//点击编辑按钮回显数据
function toRelease(templateId){
  loadTemplateIndex(templateId);
  loadTemplateProduct(templateId)
  testTemplateRequest.selectTestTemplateById(templateId, res=>{
    updateTestTemplate.value = res.data;
    updateTestTypesArr.value = updateTestTemplate.value.testTypes.split(',');
  })
  testTemplateUpdateWinDialogVisible.value = true;
}
//行编辑取消按钮
function updateDialogVisible(){
  testTemplateUpdateWinDialogVisible.value = false;
}

// 行编辑提交按钮
function updateTestTemplateSubmit(){
  updateTestTemplate.value.testTypes = updateTestTypesArr.value.join(',');
  testTemplateRequest.updateTestTemplate(updateTestTemplate.value, res=>{
    if (res.code == 200) {
      ElMessage({
        type: "success",
        message: "编辑成功"
      });
      // 关闭窗体
      updateDialogVisible();
      // 刷新表格
      loadTestTemplate();
    } else {
      ElMessage({
        type: "error",
        message: "编辑失败"
      });
    }
  })
}



//检测项行编辑弹窗标记
let templateIndexUpdateWinDialogVisible = ref(false);
//检测项保存编辑内容
let updateTemplateIndex = ref([]);


//检测项点击编辑按钮回显数据
function toReleaseTemplateIndex(recordId){
  templateIndexRequest.selectTemplateIndexById(recordId, res=>{
    updateTemplateIndex.value = res.data;
  })
  templateIndexUpdateWinDialogVisible.value = true;
}
//检测项行编辑取消按钮
function updateTemplateIndexDialogVisible(){
  templateIndexUpdateWinDialogVisible.value = false;
}

// 检测项行编辑提交按钮
function updateTemplateIndexSubmit(){
  templateIndexRequest.updateTestIndex(updateTemplateIndex.value, res=>{
    if (res.code == 200) {
      ElMessage({
        type: "success",
        message: "编辑成功"
      });
      // 关闭窗体
      updateTemplateIndexDialogVisible();
      // 刷新表格
      loadTemplateIndex(updateTestTemplate.value.templateId);
    } else {
      ElMessage({
        type: "error",
        message: "编辑失败"
      });
    }
  })
}



//产品物料行编辑弹窗标记
let templateProductUpdateWinDialogVisible = ref(false);
//产品物料保存编辑内容
let updateTemplateProduct = ref([]);


//产品物料点击编辑按钮回显数据
function toReleaseTemplateProduct(recordId){
  templateProductRequest.selectTemplateProductById(recordId, res=>{
    updateTemplateProduct.value = res.data;
  })
  templateProductUpdateWinDialogVisible.value = true;
}
//产品物料行编辑取消按钮
function updateTemplateProductDialogVisible(){
  templateProductUpdateWinDialogVisible.value = false;
}

// 产品物料行编辑提交按钮
function updateTemplateProductSubmit(){
  templateProductRequest.updateTestProduct(updateTemplateProduct.value, res=>{
    if (res.code == 200) {
      ElMessage({
        type: "success",
        message: "编辑成功"
      });
      // 关闭窗体
      updateTemplateProductDialogVisible();
      // 刷新表格
      loadTemplateProduct(updateTestTemplate.value.templateId);
    } else {
      ElMessage({
        type: "error",
        message: "编辑失败"
      });
    }
  })
}






//行删除按钮
function toDelete(templateId){
  console.log(templateId)
  testTemplateRequest.deleteTestTemplate(templateId,res=>{
    console.log(res)
    if (res.code == 200) {
      ElMessage({
        type: "success",
        message: "删除成功"
      });
      // 刷新表格
      loadTestTemplate();
    } else {
      ElMessage({
        type: "error",
        message: "删除失败"
      });
    }
  })
}


//批量删除
function toDeletes(){
  let selectRows = defectTable.value.getSelectionRows();
  if(selectRows.length == 0){
    ElMessage({
      type:"warning",
      message:"你没有选中任何数据"
    });
    return;
  }
  ElMessageBox.confirm(
      '你确定要将选中的任务删除吗',
      '提示',
      {
        confirmButtonText:'是的',
        cancelButtonText:'点错了',
        type:'warning',
      }
  ).then(()=>{
    let defectIds = [];

    selectRows.forEach(row=>{
      defectIds.push(row.defectId);
    })

    //
    defectRequest.deletes(defectIds,res=>{
      loadDefect()
      if(res.code == 200){
        ElMessage({
          type:"success",
          message:"成功删除[" + res.data + "]条数据"
        })
        loadDefect()
      }else{
        ElMessage({
          type:"warning",
          message:"删除失败"
        })
      }
    });


  }).catch(()=>{});
}


//详情弹窗标记
let testTemplateListWinDialogVisible = ref(false);
//
let rowTestTemplate = ref([]);
//
let rowTestTypeArr = ref({});

//详情
function showDetail(thisRowTestTemplate){
  rowTestTemplate.value = thisRowTestTemplate;
  rowTestTypeArr.value = rowTestTemplate.value.testTypes.split(',');
  testTemplateListWinDialogVisible.value = true;
}




// 在声明周期的onmounted函数中调用loadUsers函数
onMounted(()=>{

  loadTestTemplate();


})

</script>

<style scoped>

</style>